package com.frank.jni_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.frank.jni_demo.databinding.ActivityMainBinding;
import com.frank.sdk.Timer;

import static com.frank.sdk.MyStringUtil.ndkString;


public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";
    private ActivityMainBinding binding;

    private Timer mTimer;

    private Button mButton;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    mTimer.create();
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mButton = findViewById(R.id.btn_timer);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mTimer == null) {
                    mTimer = new Timer();
                }
                Message msg =  new Message();
                msg.what = 0;
                mHandler.sendMessage(msg);
            }
        });

        // Example of a call to a native method
        TextView tv = binding.sampleText;
        tv.setText(ndkString());


    }


    @Override
    protected void onDestroy() {
        //是将我们的方法放在这呢？
        super.onDestroy();
        //还是将我们的方法放在这呢？
        mTimer.destroy();
    }

}